<?php
	class SearchAction extends BaseAction{
		public function show(){
			//dump($_GET);
			if(!empty($_GET['type']) && !empty($_GET['inputkeyword'])){
				$art = M('Article');
				$where = array();
				if('tag' == $_GET['type']){//tag标签搜索
					$tag = M('Tag');
					
					$tagData = $tag->where('tagname = "'.$_GET['inputkeyword'].'"')->find();
// 					dump($tagData);
// 					echo $tag->getLastSql();
					if(null == $tagData){//tag表中不存在相应的tag标签，则新增
						$data = array();
						$data['tagname'] = $_GET['inputkeyword'];
						$data['count'] = 1;
						$tag->add($data);
// 						echo $tag->getLastSql();
// 						$tag->execute('insert into zgcms_tag (tagname) values ("'.$_GET['inputkeyword'].'")');
// 						echo 'insert into zgcms_tag (tagname) values "'.$_GET['inputkeyword'].'"';
					}else{//存在则搜索次数count字段加1
						$tag->where('tagname = "'.$_GET['inputkeyword'].'"')->setInc('count',1);
					}
				}
				$where[$_GET['type']] = array('like','%'.$_GET['inputkeyword'].'%');//文章搜索模糊匹配
				import("ORG.Util.Page");
				$count = $art->where($where)->count();
				$page = new Page($count,c('PAGESIZE'));
				$show = $page->show();
				$artData = $art->limit($page->firstRow.','.$page->listRows)
								->field('zgcms_article.id as id,info,category_name,title,title as t,category,date,count,keyword,tag,thumbnail')
								->join('zgcms_category on zgcms_article.category = zgcms_category.id')
								->where($where)
								->select();
// 				echo $art->getLastSql();
				
				if(null == $artData && 'tag' == $_GET['type'] && null != $tagData){//tag标签下无相应的文章并且tag标签存在，则删除之
					$tag->where('tagname = "'.$_GET['inputkeyword'].'"')->delete();
// 					echo $tag->getLastSql();
				}
				//搜索结果关键字加红显示
				foreach($artData as $key => $val){
					$artData[$key][$_GET['type']] =str_replace($_GET['inputkeyword'], '<span class="red">'.$_GET['inputkeyword'].'</span>',$artData[$key][$_GET['type']]);
				}
				$this->assign('art',$artData);
				$this->assign('page',$show);
				$this->display();
			}else{
				$this->error('搜索类型或内容为空！');
			}
			
		}
	}

?>